/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.common.configuration;

import com.inspur.edp.lcm.metadata.common.Utils;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.springframework.util.StringUtils;

/**
 * gsp工程配置文件帮助
 * @author zhongchq
 * @since 1.0
 */
public class GspProjectExtendHelper {
    private final String fileName = "config/platform/common/lcm_gspprojectextend.json";
    private final String archetypePom = "platform/dev/main/libs/resources/archetypefront/src/main/resources/archetype-resources/pom.xml";
    private final String sectionName = "MavenProjectName";
    private static GspProjectExtendHelper singleton = null;
    private String absoluteFilePath;
    private String absoluteArchetypePomPath;

    public static GspProjectExtendHelper getInstance() {
        if (singleton == null) {
            singleton = new GspProjectExtendHelper();
        }
        return singleton;
    }

    public String getMavenProjectName() {
        try {
            return MetadataServiceHelper.getConfigurationString(getAbsoluteFilePath(), sectionName);
        } catch (Exception e) {
            throw new RuntimeException("读取配置文件失败：" + getAbsoluteFilePath(), e);
        }
    }

    public String getEdpParentVersion() {
        try {
            return getEdpParentVersion(getAbsoluteArchetypePomPath());
        } catch (Exception e) {
            throw new RuntimeException("读取文件失败：" + getAbsoluteArchetypePomPath());
        }
    }

    public static String getEdpParentVersion(String pomPath) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(pomPath);
            MavenXpp3Reader reader = new MavenXpp3Reader();
            Model model = reader.read(fis);
            return model.getParent().getVersion();
        } catch (Exception e) {
            throw new RuntimeException("读取pom文件失败：" + pomPath);
        } finally {
            if (!(null == fis)) {
                fis.close();
            }
        }
    }

    private String getAbsoluteArchetypePomPath() {
        if (StringUtils.isEmpty(absoluteArchetypePomPath)) {
            absoluteArchetypePomPath = Utils.getAbsoluteConfigFilePath(archetypePom);
        }
        return absoluteArchetypePomPath;
    }

    private String getAbsoluteFilePath() {
        if (StringUtils.isEmpty(absoluteFilePath)) {
            absoluteFilePath = Utils.getAbsoluteConfigFilePath(fileName);
        }
        return absoluteFilePath;
    }
}
